
    /**  
    * @Title: LogStorageMysqlConfiguration.java
    * @Package com.efast.cafe.commponent.log.configuration
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年9月4日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.log.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;
import com.efast.cafe.commponent.log.constant.LogConstant;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.util.ConfigUtil;

/**
    * @ClassName: LogStorageMysqlConfiguration
    * @Description: 日志启用mysql存储服务配置
    * @author Administrator
    * @date 2018年9月4日
    *
    */
@ConditionalOnProperty(
        name=LogConstant.K_STORAGE,
        havingValue="mysql"
)
@Configuration
@AutoConfigureAfter(MysqlDataSourceConfigProperties.class)
public class LogStorageMysqlConfiguration {
	
	private String storage = ConfigUtil.getValue(LogConstant.K_STORAGE);
	
	@Autowired
	private MysqlDataSourceConfigProperties mysqlDataSourceConfigProperties;

	
	
	
	@Bean("mysqlLogDruidDataSource")
	public DruidDataSource mysqlLogDruidDataSource(){
		DruidDataSource datasource = new DruidDataSource();
		 /* 基础配置 */
        datasource.setUrl(this.mysqlDataSourceConfigProperties.getUrl());
        datasource.setUsername(this.mysqlDataSourceConfigProperties.getUsername());
        datasource.setPassword(this.mysqlDataSourceConfigProperties.getPassword());
        datasource.setDriverClassName(this.mysqlDataSourceConfigProperties.getDriverClassName());

        /* 其他配置 */
        datasource.setInitialSize(this.mysqlDataSourceConfigProperties.getInitialSize());
        datasource.setMinIdle(this.mysqlDataSourceConfigProperties.getMinIdle());
        datasource.setMaxActive(this.mysqlDataSourceConfigProperties.getMaxActive());
        datasource.setMaxWait(this.mysqlDataSourceConfigProperties.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(this.mysqlDataSourceConfigProperties.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(this.mysqlDataSourceConfigProperties.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(this.mysqlDataSourceConfigProperties.getValidationQuery());
        datasource.setTestWhileIdle(this.mysqlDataSourceConfigProperties.getTestWhileIdle());
        datasource.setTestOnBorrow(this.mysqlDataSourceConfigProperties.getTestOnBorrow());
        datasource.setTestOnReturn(this.mysqlDataSourceConfigProperties.getTestOnReturn());
		return datasource;
	}
	
	@Bean("mysqlLogJdbcTemplate")
	public JdbcTemplate mysqlLogJdbcTemplate(){
		JdbcTemplate JdbcTemplate = new JdbcTemplate(mysqlLogDruidDataSource());
		return JdbcTemplate;
	}
	
	@Bean("mysqlLogJdbcDao")
	public JdbcDao logJdbcDao(){
		JdbcDaoImpl jdbcDao = new JdbcDaoImpl();
		if(storage.equals("mysql"))
			jdbcDao.setJdbcTemplate(mysqlLogJdbcTemplate());
		return jdbcDao;
	}
}
